package models

import (
	"gorm.io/gorm"
	"house-src/basic/config"
)

// 订单
type Order struct {
	gorm.Model
	CustomerId    int     `json:"customer_id" gorm:"type:int;comment:'客户id'"`
	CleanerId     int     `json:"cleaner_id" gorm:"type:int;comment:'清洁师id'"`
	ReservationId int     `json:"reservation_id" gorm:"type:int;comment:'预约id'"`
	OrderSn       string  `json:"order_sn" gorm:"type:varchar(100);unique;comment:'订单编号'"`
	OrderPrice    float64 `json:"order_price" gorm:"type:decimal(10,2);comment:'订单价格'"`
	Status        int     `json:"status" gorm:"type:int;comment:'状态 1-待接单 2-已接单 3-已结束'"`
	PayMothed     int     `json:"pay_mothed" gorm:"type:int;comment:'支付方式 1-支付宝 2-微信'"`
	PayStatus     int     `json:"pay_status" gorm:"type:int;comment:'支付状态 1-未付款 2-交易超时 3-交易成功 4-交易结束'"`
}

func (o *Order) TableName() string {
	return "order"
}

// 添加订单
func (o *Order) AddOrder() error {
	return config.DB.Create(&o).Error
}

// 根据id查询订单
func (o *Order) GetOrderById(id int) error {
	return config.DB.Model(&Order{}).Where("id = ?", id).Limit(1).Find(&o).Error
}

// 更新支付状态
func (o *Order) UpdatePayStatus(orderSn string, status int) error {
	return config.DB.Model(&Order{}).Where("order_sn = ?", orderSn).Update("status", status).Find(&o).Error
}

// 根据订单号查询
func (o *Order) GetOrderInfo(orderSn string) error {
	return config.DB.Model(&Order{}).Where("order_sn = ?", orderSn).Limit(1).Find(&o).Error
}

// 更新订单状态
func (o *Order) UpdateStatus(id int) error {
	return config.DB.Model(&Order{}).Where("id = ?", id).Updates(&o).Find(&o).Error
}

// 订单列表
func (o *Order) GetOrderList(customerId, status int) (orderList []*Order, err error) {
	db := config.DB.Model(&Order{}).Where("customer_id = ?", customerId)
	if status != 0 {
		db = db.Where("status = ?", status)
	}
	db.Find(&orderList)
	return orderList, err
}

// 订单详情
func (o *Order) GetOrderDetail(customerId, orderId int) error {
	return config.DB.Model(&Order{}).Where("id = ? and customer_id = ?", orderId, customerId).Limit(1).Find(&o).Error
}
